/*
 * This program is free software: you can redistribute it and/or modify it under
 * the terms of the GNU General Public License as published by the Free Software
 * Foundation, either version 3 of the License, or (at your option) any later
 * version.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
 * details.
 *
 * You should have received a copy of the GNU General Public License along with
 * this program. If not, see <http://www.gnu.org/licenses/>.
 */
package org.merak.core;

import java.io.File;

import org.merak.core.model.Path;

public final class Paths {

	//~ Attributes ///////////////////////////////////////////////////////////////
	//****************************************************************************
	public final Path docBase         = new Path(); // Absolute path to application home
	public final Path applicationData = new Path(); // General application data
	public final Path configuration   = new Path(); // Data related to configurations
	public final Path temporary       = new Path(); // Temporary files
	public final Path log             = new Path(); // Log files
	public final Path script          = new Path(); // Script files
	public final Path content         = new Path(); // Templates, worksheets, databases, data files, etc

	//~ Constructors /////////////////////////////////////////////////////////////
	//****************************************************************************
	protected Paths() { }

	//~ Methods //////////////////////////////////////////////////////////////////
	//****************************************************************************
	protected void init(String docBase,boolean isWebApp) {

		// Auxiliary
		String separator = File.separator;
		String appData   = (isWebApp ? "WEB-INF" + separator : "") + "ApplicationData";

		// Initialize directories
		this.docBase.init         ( docBase.replace('\\',File.separatorChar)     );
		this.applicationData.init ( this.docBase.getPath(appData)                );
		this.configuration.init   ( this.applicationData.getPath("Configuration"));
		this.content.init         ( this.applicationData.getPath("Content")     );
		this.log.init             ( this.applicationData.getPath("Log")          );
		this.script.init          ( this.applicationData.getPath("Script")       );		
		this.temporary.init       ( this.applicationData.getPath("Temporary")    );

		// Ensure all directories exist
		this.docBase.createDirectory(null);
		this.applicationData.createDirectory(null);
		this.configuration.createDirectory(null);
		this.temporary.createDirectory(null);
		this.log.createDirectory(null);
		this.script.createDirectory(null);
		this.content.createDirectory(null);

	}
	//****************************************************************************

}
